<template>
  <work-template @submitOn="submitOn" v-model="ruleForm" v-loading="loading"
                 @closeSelectedTag="closeSelectedTag" :butShow="true">
    <div slot="form" class="container-div">
      <!--申请信息-->
      <new-application-info state="readonly" v-model="ruleForm"/>
      <!--处置结果-->
      <disposal-result state="write" ref="refDisposalResult" v-model="ruleForm"/>
    </div>
  </work-template>
</template>

<script>
import {vehicleDisposalDetails} from '@/api/vehicleDisposal/routineApplyFor.js'
import * as apis from "@/api/vehicleDisposal/routineHandle.js";

export default {
  name: 'SubmitResult',//提交处置结果
  components: {
    newApplicationInfo: () => import("../../components/routineDisposal/routineApplyFor/newApplicationInfo.vue"),
    disposalResult: () => import("../../components/routineDisposal/routineHandle/disposalResult.vue")
  },
  computed: {},
  watch: {},
  data() {
    return {
      ruleForm: {
        underwriterId: '',
        underwriterName: '',
        finalPrice: '',
        priceDifference: '',
        routineRemark: '',
        routineAttachmentOss: '',
        underwriterType: ''
      },
      loading: false
    };
  },
  created() {
    this.handleDetail()
  },
  mounted() {
  },
  methods: {
    async handleDetail() {
      this.loading = true
      try {
        const res = await vehicleDisposalDetails(this.$route.query.id)
        for (let key in res.data) {
          this.$set(this.ruleForm, key, res.data[key])
        }
        if (typeof this.ruleForm.underwriterType === 'string') {
          this.ruleForm.underwriterType = JSON.parse(this.ruleForm.underwriterType) || []
        }
      } catch (e) {
        console.error(e)
      } finally {
        this.loading = false
      }
    },

    async submitOn() {
      let loading = null
      try {

        await this.$refs.refDisposalResult.handleEffect();

        loading = this.$loading({
          lock: true,
          text: '保存中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });

        let objData = this._.cloneDeep(this.ruleForm);
        objData.underwriterType = JSON.stringify(objData.underwriterType);

        const res = await apis.vehicleDisposal(objData);
        if (res.code === 200) {
          this.$message.success('保存成功');
          this.closeSelectedTag();
        }
      } catch (error) {
        console.error(error);
      } finally {
        loading?.close();
      }
    },

    closeSelectedTag(view) {
      this.$tab.closePage(view).then(({visitedViews}) => {
        this.$router.push({
          path: "/VehicleDisposal/RoutineDisposal/RoutineHandleList",
        });
      });
    }
  },
};
</script>

<style scoped lang=""></style>


